<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_LocationController extends Zend_Rest_Controller
{
	public function init()
	{
		$this->_helper->viewRenderer->setNoRender(true);
	}


	public function deleteAction() {

	}

	public function getAction() {
		try{
			$location_id = $this->_getParam('param2');
			$user_id = $this->_getParam('param1');
			if($location_id != "" && $user_id != ""){
				$location_mapper = new Api_Model_LocationMapper();
				$location = $location_mapper->getLocationById($location_id, $user_id);

				$this->getresponse()->appendBody(json_encode($location));
				$this->getResponse()->setHttpResponseCode(200);
			} else{
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}

	}

	public function headAction() {

	}

	public function indexAction() {
		try{
			$location_mapper = new Api_Model_LocationMapper();
			$location = $location_mapper->getAllLocation();
			$this->getresponse()->appendBody(json_encode($location));
			$this->getResponse()->setHttpResponseCode(200);
		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function postAction() {
		try{
			$location_mapper = new Api_Model_LocationMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);

			if ($data != ""){
				$location_mapper->createLocation($data);
				throw new Exception(json_encode(array('meta' => 'Created')),201);
			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function searchlocationAction()
	{
		try{
			$location_mapper = new Api_Model_LocationMapper();
			$locationname = $this->_getParam('key');

			if ($locationname != ""){

				$location = $location_mapper->searchLocation($locationname);
				$this->getresponse()->appendBody(json_encode($location));
				$this->getResponse()->setHttpResponseCode(200);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function getnearAction()
	{
		try{
			$location_mapper = new Api_Model_LocationMapper();
			$latitude = $this->_getParam('param1');
			$longitude = $this->_getParam('param2');
			$locationname = $this->_getParam('key');

			if ($latitude != "" && $longitude != ''){
				$location = $location_mapper->searchNearLocation($latitude, $longitude, $locationname);
				$this->getresponse()->appendBody(json_encode($location));
				$this->getResponse()->setHttpResponseCode(200);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function putAction() {
		try{
			$location_mapper = new Api_Model_LocationMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);

			$location_id = $this->_getParam('id');

			if ($data['description'] != "" && $location_id != ''){
				$location_mapper->updateLocation($data, $location_id);
				throw new Exception(json_encode(array('meta' => 'OK')),200);
			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}
}
